﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;

namespace Site.Secure
{
    public partial class Edit_User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "SELECT * FROM Users WHERE userID = '" + comboUser.Text + "'";
            DataView DV = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            edtEmail.Text = DV[0]["userEmail"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["userType"].ToString() == "admin" || Session["userType"].ToString() == "root") //If the current user is a admin allow them to continue
                {
                    if (edtEmail.Text != "" && edtPass.Text != "")
                    {
                        SqlDataSource1.UpdateCommand = "UPDATE Users SET ";
                        SqlDataSource1.UpdateCommand += "userPass = '" + edtPass.Text + "', ";
                        SqlDataSource1.UpdateCommand += "userEmail = '" + edtEmail.Text + "' ";
                        SqlDataSource1.UpdateCommand += "WHERE userID='" + comboUser.Text + "'";
                        SqlDataSource1.Update();

                        Logger.Log("The User '" + Session["userID"] + "' edited the details of '" + comboUser.Text + "'", "USER EDITED", Server);
                        Response.Redirect("Default.aspx");
                    }
                    else
                        lblError.Text = "Please fill in all the required details";
                }
                else
                {
                    lblError.Text = "You are not allowed to add users, please login with an account that has full root access";
                }
            }
            catch
            {
                lblError.Text ="Your session has expired, this means that you can not perform any root tasks on this website. Please login again or restart your browser and navigate back to this page.";
            }
        }
    }
}